package com.roadjava.GDOUGym.service.impl;

import com.roadjava.GDOUGym.bean.dto.PlaceDTO;
import com.roadjava.GDOUGym.bean.dto.RecordDTO;
import com.roadjava.GDOUGym.bean.entity.PlaceDO;
import com.roadjava.GDOUGym.bean.entity.RecordDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.convert.RecordConvert;
import com.roadjava.GDOUGym.convert.SiteConvert;
import com.roadjava.GDOUGym.mapper.RecordMapper;
import com.roadjava.GDOUGym.mapper.SiteMapper;
import com.roadjava.GDOUGym.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
public class RecordServiceImpl implements RecordService {
    @Resource
    private RecordMapper recordMapper;

    @Resource
    private RecordConvert recordConvert;

    @Override
    public ResultDTO<List<RecordDTO>> find(RecordDTO dto) {
        RecordDO recordDO = recordConvert.dto2do(dto);
        System.out.println("recordDO = " + recordDO);
        List<RecordDO> dos = recordMapper.find(dto);
        List<RecordDTO> dtos = recordConvert.dos2dtos(dos);
        long total = recordMapper.selectCount(dto);
        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<String> add(RecordDTO recordDTO) {
        RecordDTO dto = new RecordDTO();
        List<RecordDO> recordDOList = recordMapper.find(dto);
        String no;
        if(recordDOList.size() == 0){
            no ="0001";
        }else {
            String endNo = recordDOList.get(recordDOList.size()-1).getUserNo();
            long total = Integer.parseInt(endNo)+1;
            // 把dto转为do对象
            no= "";
            if(total>=0 && total<10){
                no = "000"+total;
            }else if(total>=10 && total<100){
                no = "00"+total;
            }else if(total>=100 && total<1000){
                no = "0"+total;
            }else{
                no = total+"";
            }
        }

        // 把dto转为do对象
        RecordDO recordDO = recordConvert.dto2do(recordDTO);
        recordDO.setUserNo(no);
        int result = recordMapper.add(recordDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("添加成功");
        }else {
            return ResultDTO.buildSuccess("添加失败");
        }
    }

    @Override
    public ResultDTO<String> change(RecordDTO recordDTO) {
        // 把dto转为do对象
        RecordDO recordDO = recordConvert.dto2do(recordDTO);
        int result = recordMapper.change(recordDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<String> delete(RecordDTO recordDTO) {
        // 把dto转为do对象
        RecordDO recordDO = recordConvert.dto2do(recordDTO);
        int result = recordMapper.delete(recordDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<RecordDO> validateLogin(RecordDO param) {
        RecordDO recordDO = recordMapper.login(param.getUserNo());
        if (recordDO == null) {
            return ResultDTO.buildFailure("账号不存在");
        }
        if (!recordDO.getUserPassword().equals(param.getUserPassword())) {
            return ResultDTO.buildFailure("密码不正确");
        }
        return ResultDTO.buildSuccess(recordDO);
    }

    @Override
    public ResultDTO<RecordDO> loginPhone(RecordDO param) {
        RecordDO recordDO = recordMapper.loginPhone(param.getTelephone());
        if (recordDO == null) {
            return ResultDTO.buildFailure("账号不存在");
        }
        return ResultDTO.buildSuccess(recordDO);
    }

}
